package com.orange.homescreen.pil.defaultimpl.NotificationSummary;

import android.content.ContentResolver;
import android.content.Context;
import android.database.ContentObserver;
import android.net.Uri;
import android.os.Handler;

import com.orange.homescreen.pil.base.BasePILMonitor;

public class NotificationMessageMonitor extends BasePILMonitor {

	public static final Uri	URI_SMS_INBOX		= Uri.parse("content://mms-sms");

	private boolean			mFlag				= false;
	private ContentResolver	mResolver			= null;
	private ContentObserver	mMessageObserver	= null;

	public NotificationMessageMonitor(Context context) {
		super(context);
	}

	@Override
	public void onPause() {
	}

	@Override
	public void onResume() {
	}

	@Override
	public boolean onStart() {

		// if (mFlag == false) {
		if (!mFlag) {
			mResolver = mContext.getContentResolver();
			mMessageObserver = new ContentObserver(new Handler()) {

				@Override
				public boolean deliverSelfNotifications() {
					return super.deliverSelfNotifications();
				}

				@Override
				public void onChange(boolean selfChange) {
					notifyListeners();
				}

			};
			mResolver.registerContentObserver(URI_SMS_INBOX, true,
					mMessageObserver);
			mFlag = true;
		}
		return true;
	}

	@Override
	public void onStop() {
		if (mResolver != null && mMessageObserver != null) {
			mResolver.unregisterContentObserver(mMessageObserver);
		}

		mResolver = null;
		mMessageObserver = null;
		mFlag = false;
	}
}
